package com.code.petapp.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.code.petapp.databinding.ActivityVideoBinding;

public class VideoActivity extends BaseActivity {
    ActivityVideoBinding binding;
    private Handler handler = new Handler();
    private String videoPath;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityVideoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Intent intent = getIntent();
        videoPath=intent.getStringExtra("video");
        videoStart();


    }
    private void videoStart() {
        //设置播放加载路径
        binding.videoView.setWH(1080, 1920);
        binding.videoView.setVideoPath(videoPath);
        //播放
        handler.postDelayed(runnable, 0);
        binding.videoView.start();
        //循环播放
        binding.videoView.setOnPreparedListener(mp -> binding.loadingPb.setVisibility(View.GONE));
        binding.videoView.setOnCompletionListener(mp -> binding.videoView.start()) ;
        binding.videoView.setOnErrorListener((mp, what, extra) -> {
            binding.videoView.stopPlayback();
            return true; // 返回 true 表示错误已处理
        });
    }
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (binding.videoView.isPlaying()) {
                binding.seekBar.setMax(binding.videoView.getDuration());
                int current = binding.videoView.getCurrentPosition();
                binding.seekBar.setProgress(current);
            }
            handler.postDelayed(this, 100);
        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }
}